#include <fstream>
#include "cocos2d.h"
using namespace cocos2d;

#include "Td_object.h"
#include "totem_obj_config.h"
#include "animation.h"
#include "serialization_wrap.h"
#include "levelSelectScene.h"

#include "util.h"

extern void setAdShow(bool is_show, bool is_atGameScene = true);
extern Texture2D* getCombineTex(Texture2D *texture, CGSize win, boost::function<void()> callback);
scoped_object_c_ptr<Texture2D> m_bgTex;

menu_item_info_t g_itemRestart;
menu_item_info_t g_itemBack2Menu;
menu_item_info_t g_itemLevel;
menu_item_info_t g_itemSoundEnable;
menu_item_info_t g_itemSoundDisable;

CGPoint g_level_pos;
CGPoint g_time_pos;
CGPoint g_bomb_pos;
image_info_t IMAGE_PASS;
image_info_t IMAGE_CHECK;
image_info_t IMAGE_GAME_OVER;
image_info_t IMAGE_TIME;
image_info_t IMAGE_LEVEL;
image_info_t IMAGE_BOMB;
const image_info_t IMAGE_GET = {{SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 50}, "game_get.png"};
const image_info_t IMAGE_READY = {{SCREEN_WIDTH/2, SCREEN_HEIGHT/2-10 }, "game_get_ready.png"};
const image_info_t IMAGE_GO = {{SCREEN_WIDTH/2 , SCREEN_HEIGHT/2 }, "game_go.png"};
const image_info_t IMAGE_WIN = {{SCREEN_WIDTH/2 , SCREEN_HEIGHT/2 }, "congratulations.png"};


#ifdef DEBUG
const image_info_t IMAGE_AIM = {{SCREEN_WIDTH/2 + 80, SCREEN_HEIGHT/2}, "game_aim.png"};
#endif

const char * const BIG_ALPHA_TABLE_FILE_NAME = "big_numbers.png";
const char * const SMAIL_ALPHA_TABLE_FILE_NAME = "smail_numbers.png";

void init_game_scene_xml()
{
	using namespace std;
//	string filename(getResPath("game_scene.b"));
//	std::ifstream ifs(filename.c_str());
//	boost::archive::binary_iarchive ar(ifs);
	string filename(getResPath("game_scene.xml"));
	std::ifstream ifs(filename.c_str());
	boost::archive::xml_iarchive ar(ifs);
//	
//	ar & BOOST_SERIALIZATION_NVP(g_itemRestart);
//	ar & BOOST_SERIALIZATION_NVP(g_itemBack2Menu);
//	ar & BOOST_SERIALIZATION_NVP(g_itemLevel);
//	ar & BOOST_SERIALIZATION_NVP(g_itemSoundEnable);
//	ar & BOOST_SERIALIZATION_NVP(g_itemSoundDisable);
//	
//	ar & BOOST_SERIALIZATION_NVP(g_level_pos);
//	ar & BOOST_SERIALIZATION_NVP(g_time_pos);
//	ar & BOOST_SERIALIZATION_NVP(g_bomb_pos);
//	ar & BOOST_SERIALIZATION_NVP(IMAGE_PASS);
//	ar & BOOST_SERIALIZATION_NVP(IMAGE_CHECK);
//	ar & BOOST_SERIALIZATION_NVP(IMAGE_GAME_OVER);
//	ar & BOOST_SERIALIZATION_NVP( IMAGE_TIME );
//	ar & BOOST_SERIALIZATION_NVP( IMAGE_LEVEL );
//	ar & BOOST_SERIALIZATION_NVP( IMAGE_BOMB );
	
	{
//	string filename(getResPath("game_scene.b"));
//	std::ifstream ifs(filename.c_str());
//	boost::archive::binary_iarchive ar(ifs);
//		string filename(("game_scene.b"));
//		std::ofstream fs(filename.c_str());
//		boost::archive::binary_oarchive ar(fs);

		ar & BOOST_SERIALIZATION_NVP(g_itemRestart);
		ar & BOOST_SERIALIZATION_NVP(g_itemBack2Menu);
		ar & BOOST_SERIALIZATION_NVP(g_itemLevel);
		ar & BOOST_SERIALIZATION_NVP(g_itemSoundEnable);
		ar & BOOST_SERIALIZATION_NVP(g_itemSoundDisable);
		
		ar & BOOST_SERIALIZATION_NVP(g_level_pos);
		ar & BOOST_SERIALIZATION_NVP(g_time_pos);
		ar & BOOST_SERIALIZATION_NVP(g_bomb_pos);
		ar & BOOST_SERIALIZATION_NVP(IMAGE_PASS);
		ar & BOOST_SERIALIZATION_NVP(IMAGE_CHECK);
		ar & BOOST_SERIALIZATION_NVP(IMAGE_GAME_OVER);
		ar & BOOST_SERIALIZATION_NVP( IMAGE_TIME );
		ar & BOOST_SERIALIZATION_NVP( IMAGE_LEVEL );
		ar & BOOST_SERIALIZATION_NVP( IMAGE_BOMB );
	}
	
}

inline void setLevelLabel(scoped_object_c_ptr<LabelAtlas> &level_lable)
{
	static int previous_value = 0;
	if (previous_value != game_logic::CurLevel() || NULL == level_lable.get()) {
		string lable;
		//NSString *string = [NSString stringWithFormat:@"%d", game_logic::CurLevel()+1];
		if (NULL == level_lable.get()) {
			level_lable.
			level_lable.reset([LabelAtlas labelAtlasWithString:string charMapFile:SMAIL_ALPHA_TABLE_FILE_NAME itemWidth:17 itemHeight:22 startCharMap:'0']);
			[level_lable.get() retain];
		}
		else
			[level_lable.get() setString:string];
		previous_value = game_logic::CurLevel();
	}
}

//inline void setDigitalLabel(scoped_object_c_ptr<LabelAtlas> &lable, int num)
//{	
//	NSString *string = [NSString stringWithFormat:@"%d", num];
//	if (NULL == lable.get()) {
//		lable.reset([LabelAtlas labelAtlasWithString:string charMapFile:SMAIL_ALPHA_TABLE_FILE_NAME itemWidth:17 itemHeight:22 startCharMap:'0']);
//		[lable.get() retain];
//	}
//	else
//		[lable.get() setString:string];
//}

//inline void setStepsLabel(scoped_object_c_ptr<LabelAtlas> &steps_lable)
//{
//	static int previous_value = 0;
//	if (previous_value != game_logic::remainSteps() || NULL == steps_lable.get()) {
//		
//		NSString *string = [NSString stringWithFormat:@"%d", game_logic::remainSteps()];
//		if (NULL == steps_lable.get()) {
//			steps_lable.reset([LabelAtlas labelAtlasWithString:string charMapFile:BIG_ALPHA_TABLE_FILE_NAME itemWidth:31 itemHeight:38 startCharMap:'0']);
//			//FPSLabel = [[LabelAtlas labelAtlasWithString:@"00.0" charMapFile:@"fps_images.png" itemWidth:16 itemHeight:24 startCharMap:'.'] retain];
//			[steps_lable.get() retain];
//		}
//		else
//			[steps_lable.get() setString:string];
//		previous_value = game_logic::remainSteps();
//	}
//}
